Skip to content

cmp, diffutils: handle stdout write errors without panicking (#165) - #442

Open
yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:fix/cmp-and-multicall-dev-full-panic
Open

yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:fix/cmp-and-multicall-dev-full-panic

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

When writing to a full or unbuffered stdout device (such as /dev/full), cmp and the multicall binary diffutils panicked inside Rust's println! macro with failed printing to stdout: No space left on device (os error 28) (exit status 134).

Changes

  • In src/cmp.rs: Write differences and verbose output directly to the locked stdout writer, reporting write errors as cmp: standard output: <error> with exit status 2 (matching GNU cmp and diff).
  • Safely handle stdout write errors in cmp --help without panicking.
  • In src/main.rs: Safely write usage text without panicking when stdout is full.
  • Add regression tests in tests/by-util/test_cmp.rs for default cmp, verbose cmp (-l), help output, and the multicall binary.

Fixes #165

Comment thread src/main.rs
let written = std::io::Write::write_all(&mut out, s.as_bytes())
.and_then(|()| std::io::Write::flush(&mut out));
if let Err(e) = written {
if e.kind() != std::io::ErrorKind::BrokenPipe {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the write error is printed but we still exit 0 via second_arg_error, no?
should be 2 like cmp/diff

Comment thread src/main.rs
if e.kind() != std::io::ErrorKind::BrokenPipe {
let _ = std::io::Write::write_fmt(
&mut std::io::stderr().lock(),
format_args!("{name}: standard output: {e}\n"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this keeps the "(os error 28)" suffix, cmp and diff strip it
could you please reuse format_failure_to_read_input_file?

Comment thread src/main.rs
);
let mut out = std::io::stdout().lock();
let written = std::io::Write::write_all(&mut out, s.as_bytes())
.and_then(|()| std::io::Write::flush(&mut out));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use std::io::Write; instead of the fully qualified calls

Comment thread tests/by-util/test_cmp.rs
.stdout(File::create("/dev/full").unwrap())
.output()
.unwrap();
assert_ne!(output.status.code(), Some(134));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too weak, please check the exit code and the stderr message

Comment thread src/cmp.rs
writeln!(out)
}

fn stdout_error(executable: &std::ffi::OsStr, err: &io::Error) -> String {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning an empty string for broken pipe and testing is_empty() in two places is fragile
could be a dedicated error variant, no?

Comment thread src/cmp.rs
String::new()
} else {
format_failure_to_read_input_file(
&OsString::from(executable),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params.executable is already an OsString, why take &OsStr and convert back?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diffutils > /dev/full panics

2 participants